<?php
session_start();
include_once 'libmaster/parse.php';
include_once('libmaster/parseUser.php');
/** should save data **
 * {"username":"handy",
 * "email":"lanhuonga3@gmail.com",
 * "createdAt":"2012-11-19T12:01:53.647Z",
 * "updatedAt":"2012-11-19T12:01:53.647Z",
 * "objectId":"HCqXU6tFoY",
 * "sessionToken":"maou9dww7zk7h461geka7jqql"}
 * */
 if(isset($_SESSION['userdata'])){
    echo "1";
    return;
 }
switch($_GET['action']){
    case 'login':{
        $parse = new parseUser();
        if(isset($_POST['username'])&&isset($_POST['password'])){
            
            $parse->data = array('username'=>$_POST['username'],'password'=>$_POST['password']);
            $data = json_decode($parse->login());
            if(isset($data['sessionToken'])){
                //success
                $_SESSION['userdata'] = $data;
                echo "1";
            }
            else
            echo "-1";
        }
        else{
            echo '-1';
        }
    }
    break;
    case 'logout':{
        session_destroy();
        echo '1';
    }
    break;
    default: {
        
    }
    break;
}
?>